import navigator from "../../module/navigator";
import sessionModule from "../../module/sessionStorage";
import customAPI from "@/api/custom";
// import Router from '@/router'
export default {
  // 设置导航数据（后端数据格式话，返回原数据经过格式化后的数组，未经过树处理）
  setNavigatorMenuOriginData(state, data) {
    // 将导航数据存入store
    state.navigatorMenuList = data;
    // 将导航数据存入session
    sessionModule.save({
      key: "TardeMenusData",
      value: data,
    });
  },

  // 设置用户信息
  setUserInfo(state, data) {
    state.userInfo = data;
    sessionModule.save({
      key: "TardeUserInfo",
      value: data,
    });
  },
  // 重置用户信息
  resetUserInfo(state) {
    state.userInfo = {};
  },

  // 设置配置项数据
  setCustomTreeOriginData(state, data) {
    // 将导航数据存入store
    state.customList = data;
    // 将导航数据存入session
    sessionModule.save({
      key: "TardeCustomListData",
      value: data,
    });
  },
  // 重置配置项数据
  resetCustomTreeOriginData(state) {
    state.customList = [];
  },
  // 修改配置项数据
  updateCustomSetting(state, data) {
    let { GroupKey, SetName, value } = data;
    let GroupIndex = state.customList.findIndex((i) => i.key == GroupKey);
    if (GroupIndex == -1) {
      console.log({ code: 500, msg: "修改配置失败,分组不存在" });
      return;
    }
    let SettingList = state.customList[GroupIndex].setting;
    let SettingIndex = SettingList.findIndex((i) => i.name == SetName);
    if (SettingIndex == -1) {
      console.log({ code: 500, msg: "修改配置失败,配置不存在" });
      return;
    }
    let SettingFind = SettingList[SettingIndex];
    let { type } = SettingFind;
    customAPI
      .updateUserCustomSetting({
        type,
        GroupKey,
        SetName,
        value,
      })
      .then(() => {
        state.customList[GroupIndex].setting[SettingIndex].value = value;
      })
      .catch((err) => {
        console.log(err);
      });

    // let { type } = SettingFind;
    // if (type == 0) { // 下拉数据
    //   state.customList[GroupIndex].setting[SettingIndex].value = value;
    // }
    // if (type == 1) { // 开关
    //   state.customList[GroupIndex].setting[SettingIndex].value = value
    // }
  },

  // 新增菜单
  addMenuBar(state, form) {
    let originForm = JSON.parse(JSON.stringify(form));
    // 格式化数据
    let data = navigator.addMenuForm(originForm);
    // Push到Vuex
    state.navigatorMenuList.push(data);
  },
  // 更改选中菜单的ID- 专业版
  changeNavigatorCurrentId(state, data) {

    console.log("执行AAAA");

    let { id, ifMenuBar } = data;
    if (ifMenuBar) {
      let list = state.navigatorMenuList;
      let index = state.navigatorMenuList.findIndex((e) => e.meta.id == id);
      let menu = state.navigatorMenuList.find((e) => e.meta.id == id);
      let childrenDevelop = menu.meta.display.childrenDevelop;
      list[index].meta.display.childrenDevelop = !childrenDevelop;
    } else {
      state.currentNavigatorId = id;
    }
  },
  changeIconNavigatorCurrentId(state, data) {
    let { id } = data;
    state.currentIconNavigatorId = id;
  },
  changeHeadersOptionsSort(state, data) {
    let { newSort } = data;
    let newList = newSort.map((id) => {
      return state.layout.header.options.list.find((item) => item.id == id);
    });
    state.layout.header.options.list = [...newList];
  },
  changeHeadersOptionsStatus(state, data) {
    let { id, disable } = data;

    let newList = state.layout.header.options.list.map((item) => {
      if (item.id == id && item.sureDisable) {
        item.disable = disable;
      }
      return item;
    });
    state.layout.header.options.list = [...newList];
  },
  // 更改主题
  updateCurrentTheme(state, data) {
    state.theme.current = data.current;
  },
  // 更改侧边栏收缩（显示）状态
  changeNavigatorShow(state, data) {
    let { newStatus } = data;
    state.navigatorShow = newStatus;
  },
  // 更改导航菜单Item 元信息
  changeNavigatorItemMeta(state, data) {
    let { id, meta } = data;
    let index = state.navigatorMenuList.findIndex((item) => item.id == id);
    let OriginMeta = state.navigatorMenuList[index].meta;
    Object.assign(OriginMeta, meta);
  },
};
